import {
	BASE_URL
} from '@/config.js'

const httpInterceptor = {
	// 拦截前触发
	invoke(options) {
	 
			
	 
		options.sslVerify = false;
		options.withCredentials = true;
		// 1. 非 http 开头需拼接地址
		if (!options.url.startsWith('http')) {
			options.url = BASE_URL + options.url
		}
		// 2. 请求超时, 默认 60s
		if (options.timeout == undefined) {
			options.timeout = 1800000
		}
		// 3. 添加小程序端请求头标识

		options.header = {
			'source-client': 'uniapp',
			//	'content-Type': 'application/x-www-form-urlencoded',
			...options.header,
		}
		if (options.header['content-Type'] == undefined) {
			options.header['content-Type'] = 'application/x-www-form-urlencoded';
		}
		options.header["Keep-Alive"] = "timeout=1800000";
		options.header['token'] = uni.getStorageSync(uni.$Call.token);
		options.header['ca-token'] = Date.now();
		options.header['timelong'] = new Date().getTime();
		options.header['platform'] =  uni.getSystemInfoSync().platform;
		options.header['version'] =   uni.getStorageSync("version");


		//console.log("options",options)
		// if (options.url != undefined && options.url == "undefined" ) {
		// 	return;
		// }
		//console.log("请求地址:", options.url)
		//console.log("请求参数:", options.data)
	},
	returnValue(data) {
		// 只返回 data 字段
		//console.log("接口返回信息:",data);
		return data
	}
}

uni.addInterceptor('request', httpInterceptor)
uni.addInterceptor('uploadFile', httpInterceptor)

export const request = (options) =>
	new Promise((resolve, reject) => {
		// console.log("options2",options)
		uni.request({
			...options,
			// 响应成功
			success(res) {
				console.log("请求信息地址:", options.url)
				console.log("请求参数:",options.data) 
				console.log("接口返回信息:", res)
				// console.log( "------------------------------------------") 
				if (res.statusCode === 200) {
					
					if (options.url != 'user/updatePassWord' && (res.data.code == 501 || uni.getStorageSync(
							"resetPassword") == '1')) {
						//密码是默认的需要进行修改
						uni.redirectTo({
							url: '/pages/password/setANewPassword'
						});
						return;
					}
					if (res.data.code == 222222 || res.data.code == '222222') {
						console.log("---- 重复提交");
						return;
					}
					if (res.data.code == 0 || res.data.code == '0') {
						//console.log("成功回调")
						resolve(res)
					} else {
						if (res.data.message) {
							uni.showToast({
								icon: 'error',
								title: res.data.message,
								position: 'bottom'
							})
						}
						if (res.data.code === 401) {
							uni.hideLoading();
							uni.removeStorageSync(uni.$Call.token)

							// 跳转到登录页面
							uni.reLaunch({
								url: '/pages/land/land'
							});
							return;
						}
						//console.log('错误信息:',res.data)
						uni.$emit('z-paging-error-emit');
						resolve(res)
					}
				} else {
					//console.log('错误信息statusCode:',res.data)
					uni.$emit('z-paging-error-emit');
					resolve(res)
				}
			},
			// 响应失败
			fail(err) {
				uni.hideLoading();
				reject(err)
				uni.$emit('z-paging-error-emit');
				// uni.showToast({
				// 	icon: 'none',
				// 	title: '网络错误，换个网络试试',
				// 	duration: 3000,
				// })
			},
		})
	})